package com.vision.admin.sv.serv.rpc;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.springframework.stereotype.Service;

import com.vision.admin.cl.AdminTasks;
import com.vision.core.cm.data.AdminPermissions;
import com.vision.core.cm.data.TaskNode;
import com.vision.core.cm.db.data.Permission;
import com.vision.core.cm.exception.RpcException;
import com.vision.core.cm.serv.rpc.MenuService;
import com.vision.core.sv.serv.BaseRpcServlet;

/**
 * 
 * @author Mark
 * 
 */
@Service("menu")
public class MenuServiceImpl extends BaseRpcServlet implements MenuService {

	private static final long serialVersionUID = 1L;
	
	public MenuServiceImpl() {
		initGilead();
	}

	@Override
	public List<TaskNode> getMenu() throws RpcException {
		Set<Permission> permissions = getLoggedInUsersPermissions();
		
		List<TaskNode> list = new ArrayList<TaskNode>();
		
		{
			TaskNode masterlist = new TaskNode(AdminTasks.MASTERLIST);
			
			if ( permissions.contains(AdminPermissions.ViewCompany) )
				masterlist.addChildren(new TaskNode(AdminTasks.COMPANIES));
			
			if ( permissions.contains(AdminPermissions.ViewUser) )
				masterlist.addChildren(new TaskNode(AdminTasks.USERS));
			
			if ( permissions.contains(AdminPermissions.ViewRole) )
				masterlist.addChildren(new TaskNode(AdminTasks.ROLES));
			
			if ( permissions.contains(AdminPermissions.ViewOwnProfile) )
				masterlist.addChildren(new TaskNode(AdminTasks.PROFILE));
	
			// Add the parent node only if it has children
			if (masterlist.hasChildren()) {
				list.add(masterlist);
			}
		}
		

		{
			TaskNode maintenance = new TaskNode(AdminTasks.MAINTENANCE);
			
			if ( permissions.contains(AdminPermissions.ViewActivityLogs))
				maintenance.addChildren(new TaskNode(AdminTasks.ACTIVITY_LOG));
			
			if (maintenance.hasChildren()) {
				list.add(maintenance);
			}
		}

		return list;
	}

}
